//
//  FQDMobileRegisterViewController.m
//  FakeQDaily
//
//  Created by mewxiv on 9/6/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDMobileRegisterViewController.h"
#import "FQDMailRegisterViewController.h"
#import "FQDLoginViewController.h"
#import "FQDNavigationController.h"
#import "FQDInfoWebViewController.h"
#import "FQDAccountInputField.h"
#import "FQDThirdLoginView.h"
#import "FQDNavigationView.h"
#import "QDailyAPIConstants.h"

#import "Masonry.h"
#import "MBProgressHUD+FQDExtension.h"

@interface FQDMobileRegisterViewController ()

// QIcon
@property (nonatomic, strong) UIImageView *qIconImageView;
// 手机号码输入框
@property (nonatomic, strong) FQDAccountInputField *phoneInputField;
// 验证码输入框
@property (nonatomic, strong) FQDAccountInputField *verifyInputField;
// 发送验证码按钮
@property (nonatomic, strong) UIButton *sendCodeButton;
// 同意勾选框
@property (nonatomic, strong) UIButton *agreeCheckButton;
// 同意协议按钮
@property (nonatomic, strong) UIButton *agreeButton;
// 同意协议按钮分割线
@property (nonatomic, strong) UIView *agreeButtonSeparateLine;
// 下一步按钮
@property (nonatomic, strong) UIButton *nextStepButton;
// 使用邮箱注册按钮
@property (nonatomic, strong) UIButton *mailRegisterButton;
// 已有QDaily帐号按钮
@property (nonatomic, strong) UIButton *hasRegisteredButton;

@end

@implementation FQDMobileRegisterViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.hideKeyboardOnTap = YES;
    [self setupTopUI];
    [self setupMiddleUI];
    [self setupBottomUI];
}

- (void)setupTopUI
{
    self.navView.titleLabel.text = @"注册新账号";
    UIImage *closeImage = [UIImage imageNamed:@"closed"];
    [self.navView.backButton setImage:closeImage forState:UIControlStateNormal];
    [self.navView.backButton addTarget:self action:@selector(dismiss:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIImage *iconImage = [UIImage imageNamed:@"c_Qdaily button"];
    self.qIconImageView = [[UIImageView alloc] init];
    self.qIconImageView.image = iconImage;
    [self.view addSubview:self.qIconImageView];
    
    WeakSelf;
    
    [self.qIconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).offset(90);
        make.centerX.equalTo(weakSelf.view);
        make.size.equalTo(@80);
    }];
}

- (void)setupMiddleUI
{
    self.phoneInputField = [[FQDAccountInputField alloc] init];
    [self.phoneInputField setPlaceHolderString:@"手机号码(+86)"];
    [self.view addSubview:self.phoneInputField];
    
    UITextField *phoneTextField = self.phoneInputField.textField;
    phoneTextField.keyboardType = UIKeyboardTypePhonePad;
    
    self.verifyInputField = [[FQDAccountInputField alloc] init];
    [self.verifyInputField setPlaceHolderString:@"验证码"];
    [self.view addSubview:self.verifyInputField];
    
    UITextField *verifyTextField = self.verifyInputField.textField;
    verifyTextField.keyboardType = UIKeyboardTypePhonePad;
    
    WeakSelf;
    [self.phoneInputField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.qIconImageView.mas_bottom).offset(37);
        make.left.and.right.equalTo(weakSelf.view);
        make.height.equalTo(@44);
    }];
    
    [self.verifyInputField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.phoneInputField.mas_bottom);
        make.left.and.right.equalTo(weakSelf.view);
        make.height.equalTo(@44);
    }];
    
    self.sendCodeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.sendCodeButton addTarget:self action:@selector(sendCode:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.sendCodeButton];
    
    [self.sendCodeButton setTitle:@"发送验证码" forState:UIControlStateNormal];
    self.sendCodeButton.titleLabel.font = FQDRegularFontWithSize(13.0f);
    
    
    [self.sendCodeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weakSelf.view).offset(-10);
        make.bottom.equalTo(weakSelf.verifyInputField).offset(-10);
    }];
    
    self.agreeCheckButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.agreeCheckButton setImage:[UIImage imageNamed:@"agree"] forState:UIControlStateNormal];
    [self.view addSubview:self.agreeCheckButton];
    
    [self.agreeCheckButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.verifyInputField.mas_bottom).offset(7);
        make.left.equalTo(weakSelf.view).offset(10);
        make.size.equalTo(@20);
    }];
    
    self.agreeButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.agreeButton addTarget:self action:@selector(presentAgreement:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.agreeButton];
    
    [self.agreeButton setTitle:@"我已阅读并同意《用户注册协议》" forState:UIControlStateNormal];
    [self.agreeButton setTitleColor:[UIColor colorWithWhite:0.7f alpha:0.7f] forState:UIControlStateNormal];
    self.agreeButton.titleLabel.font = FQDRegularFontWithSize(13.0f);
    
    [self.agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.agreeCheckButton.mas_right);
        make.centerY.equalTo(weakSelf.agreeCheckButton);
    }];
    
    self.agreeButtonSeparateLine = [[UIView alloc] init];
    self.agreeButtonSeparateLine.backgroundColor = RGBA(178, 178, 178, 128);
    [self.view addSubview:self.agreeButtonSeparateLine];
    
    [self.agreeButtonSeparateLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.agreeButton.mas_bottom).offset(-6);
        make.right.equalTo(weakSelf.agreeButton).offset(1);
        make.size.mas_equalTo(CGSizeMake(107, 1));
    }];
    
    self.nextStepButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.nextStepButton setImage:[UIImage imageNamed:@"Next Button"] forState:UIControlStateNormal];
    [self.nextStepButton addTarget:self action:@selector(gotoNextStep:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.nextStepButton];
    
    [self.nextStepButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.verifyInputField.mas_bottom).offset(44);
        make.centerX.equalTo(weakSelf.view);
        make.size.mas_equalTo(CGSizeMake(163, 44));
    }];

 
}

- (void)setupBottomUI
{
    WeakSelf;
    self.mailRegisterButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.mailRegisterButton addTarget:self action:@selector(registerByMail:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.mailRegisterButton];
    
    [self.mailRegisterButton setTitle:@"使用邮箱注册" forState:UIControlStateNormal];
    [self.mailRegisterButton setTitleColor:[UIColor colorWithWhite:0.6f alpha:1.0f] forState:UIControlStateNormal];
    self.mailRegisterButton.titleLabel.font = FQDRegularFontWithSize(14.0f);
    
    [self.mailRegisterButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.nextStepButton.mas_bottom).offset(11);
        make.width.equalTo(weakSelf.nextStepButton);
        make.centerX.equalTo(weakSelf.view);
    }];
    
    self.hasRegisteredButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.hasRegisteredButton addTarget:self action:@selector(login:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.hasRegisteredButton];
    
    [self.hasRegisteredButton setTitle:@"已有 QDaily 帐号？" forState:UIControlStateNormal];
    [self.hasRegisteredButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.hasRegisteredButton.titleLabel.font = FQDRegularFontWithSize(12.0f);
    
    [self.hasRegisteredButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.view);
        make.bottom.equalTo(weakSelf.view).offset(-15);
    }];
    
    CGRect loginFrame = CGRectMake(40, 431, 240, 100);
    FQDThirdLoginView *loginView = [[FQDThirdLoginView alloc] initWithFrame:loginFrame];
    [self.view addSubview:loginView];
}

#pragma mark - Event Respponse
- (void)dismiss:(id)sender
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)sendCode:(id)sender
{
    [MBProgressHUD showMessage:@"发送验证码功能不可用"];
}

- (void)gotoNextStep:(id)sender
{
    [MBProgressHUD showMessage:@"手机注册功能不可用，请使用邮箱注册"];
}

- (void)registerByMail:(id)sender
{
    FQDMailRegisterViewController *mrVC = [[FQDMailRegisterViewController alloc] init];
    mrVC.blurBgImage = self.blurBgImage;
    [self.navigationController pushViewController:mrVC animated:YES];
}

- (void)presentAgreement:(id)sender
{
    FQDInfoWebViewController *iwVC = [[FQDInfoWebViewController alloc] init];
    iwVC.title = @"用户注册协议";
    iwVC.urlString = QDailyRegisterAgreementURL;
    
    FQDNavigationController *navController = [[FQDNavigationController alloc] init];
    navController.viewControllers = @[iwVC];
    [self presentViewController:navController animated:YES completion:nil];
}

- (void)login:(id)sender
{
    FQDLoginViewController *loginVC = [[FQDLoginViewController alloc] init];
    loginVC.blurBgImage = self.blurBgImage;
    [self.navigationController pushViewController:loginVC animated:YES];
}

@end
